﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_ActivationUserList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            if (!_User.isSurrogate)
            {
                PublicFunction.GoError(ErrorNumber.Unknow, "对不起，您不是代理商用户，没有此项操作功能", this.GetType().BaseType.FullName);

                return;
            }            

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/ActivationUserList.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = null;

        if (_User.ID == 1)
        {
            dt = DAL.Views.V_Users.Select("", "isActivation = 0", "");
        }
        else
        {
            dt = DAL.Views.V_Users.Select("", "isActivation = 0 and (ParentID = " + _User.ID + " or SurrogateID = " + _User.ID + " or ID = " + _User.ID + ") ", "");
        }

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            if (_User.ID != 1)
            {
                e.Item.Cells[3].Text = "****";
            }

            e.Item.Cells[9].Text = "";

            int UserType = ShoveEL._Convert.StrToInt(e.Item.Cells[20].Text, 1);     

            for (int i = 0; i < UserType; i++)
            {
                e.Item.Cells[9].Text += "<img src='../Images/gold.gif' border='0'>";
            }

            e.Item.Cells[10].Text = ShoveEL._Convert.StrToDateTime(e.Item.Cells[10].Text, System.DateTime.Now.ToString()).ToShortDateString();
            e.Item.Cells[11].Text = ShoveEL._Convert.StrToDateTime(e.Item.Cells[11].Text, System.DateTime.Now.ToString()).ToShortDateString();

            CheckBox cb = (CheckBox)e.Item.Cells[14].FindControl("cbSurrogate");
            cb.Checked = ShoveEL._Convert.StrToBool(e.Item.Cells[19].Text, true);

            double money = ShoveEL._Convert.StrToDouble(e.Item.Cells[15].Text, 0);
            e.Item.Cells[15].Text = money.ToString("N");
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnSerch_Click(object sender, EventArgs e)
    {
        if (this.tbSerch.Text.Trim().Equals(""))
        {
            ShoveEL._Web.JavaScript.Alert(this, "请输入要搜索的会员ID");

            return;
        }

        User user = new User();
        user.Name = this.tbSerch.Text.Trim();
        string ret = "";
        int result = user.GetUserInfoByName(ref ret);

        if (result == -1)
        {
            ShoveEL._Web.JavaScript.Alert(this, "此会员不存在");
            return;
        }

        DataTable dt = DAL.Views.V_Users.Select("", "isActivation = 0 and SurrogateID = " + _User.ID.ToString() + " and [ID] = " + user.ID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        gPager.PageIndex = 0;

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void btnSerchAll_Click(object sender, EventArgs e)
    {
        gPager.PageIndex = 0;

        BindData();
    }

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        long UserID = ShoveEL._Convert.StrToLong(e.Item.Cells[18].Text, -1);

        string Return = "";
        int Result = 0;

        if (e.CommandName == "Activation")
        {
            if (DAL.Procedures.P_ActivationUser.Call(UserID, _User.ID, ref Result, ref Return) < 0)
            {
                PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

                return;
            }

            if (Result < 0)
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, Return);

                return;
            }

            BindData();

            return;
        }

        if (e.CommandName == "EditUserNet")
        {
            Response.Redirect("EditUserNet.aspx?id=" + UserID.ToString(), true);

            return;
        }

        if (e.CommandName == "Del")
        {
            if (DAL.Procedures.P_DelUser.Call(UserID, ref Result, ref Return) < 0)
            {
                PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

                return;
            }

            if (Result < 0)
            {
                ShoveEL._Web.JavaScript.Alert(this.Page, Return);

                return;
            }

            BindData();

            return;
        }
    }
}